#include<bits/stdc++.h>
#define int long long
#define ls(x) ((x)*2)
#define rs(x) ((x)*2+1)
#define Debug(...) fprintf(stderr, __VA_ARGS__)
#define For(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define Rof(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
using namespace std;
const int N=1e6+5,Mod=998244353;
//typedef __int128_t i128;
//i128 _base=1;
//inline int mol(int x){return x-Mod*(_base*x>>64);}
//inline void Add(int &x,int y){x=mol(x+y+Mod);}
inline int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
int n,m;
int x[N],y[N],w[N];
int su[N],tot;
signed main(){
	//_base=(_base<<64)/Mod;
	freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout);
	// ios::sync_with_stdio(false);
	// cin.tie(0); cout.tie(0);
	For(i,2,60000){
		int p=0;
		For(j,2,(int)sqrt(i)){
			if(i%j==0) p++;
		}
		if(p) continue;
		if(i<=9||i%20==3) su[i]=1;
	}
//	For(i,1,tot) cout<<su[i]<<endl;
	int T=read();
	while(T--){
		n=read(),m=read();
		For(i,1,n) x[i]=read(),y[i]=read();
		For(i,1,n) w[i]=read();
		int ans=0;
		For(i1,1,n){
			For(i2,i1+1,n){
				For(i3,i2+1,n){
					int xx=abs(x[i1]-x[i2])+abs(y[i1]-y[i2]);
					int yy=abs(x[i1]-x[i3])+abs(y[i1]-y[i3]);
					int zz=abs(x[i3]-x[i2])+abs(y[i3]-y[i2]);
					if(su[xx+yy+zz-min(xx,min(yy,zz))-max(xx,max(yy,zz))]) ans+=w[i1]+w[i2]+w[i3];
				}
			}
		}
		printf("%lld\n",ans);
	}
#ifdef LOCAL
    Debug("\nMy Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}



